/*
2022-3-13
https://www.acwing.com/problem/content/4316/
*/ 
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;
int n;
int w[2050];
int ans;

int dfs(int u)
{
    if(u*2>(1<<n+1)-1) return 0;
    
    int l=dfs(u*2)+w[u*2];
    int r=dfs(u*2+1)+w[u*2+1];
    
    ans+=abs(l-r);
    
    return max(l,r);
}

int main()
{
    cin>>n;
    for(int i=2;i<=(1<<n+1)-1;i++) cin>>w[i];
    dfs(1);
    
    cout<<ans<<endl;
    return 0;
}
